<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
require_once ('./application/controllers/authenticate.php');
class Manga extends Authenticate {
	
	function __construct() {
		parent::__construct ();
		$this->load->model ( 'manga_model' );
		$this->load->model ( 'category_model' );
		$this->load->model ( 'manga_type_model' );
		$this->load->model ( 'manga_national_type_model' );
		$this->load->library ( 'html/control/fx_control' );
		$this->load->library ( 'html/control/fx_filter' );
		$this->load->library ( 'html/control/fx_sort' );
		$this->manga_model->addAttributeOrder ( 'id', 'desc' );
		$this->load->library ( 'upload' );
	}
	
	protected function _eventLoadLayout() {
		$this->page ['title'] = 'Manga Management';
		$this->module ['title'] = 'Manga Management';
		$this->module ['grid'] ['records'] = 20;
		$this->module ['grid'] ['page_range'] = 5;
	}
	
	function index() {
		$data = $this->_initGridView ();
		$this->setPageContent ( 'catalog/manga/grid', $data );
		$this->renderLayout ();
	}
	
	protected function _initGridView() {
		
		// Get Paginator Params
		$page = $this->input->getParam ( 'page' );
		$recordPerPage = $this->input->getParam ( 'recordPerPage' );
		if (! $recordPerPage) {
			$recordPerPage = $this->module ['grid'] ['records'];
		}
		
		// Get Filter Params
		$filters = $this->input->post ( 'mangaFilter' );
		// Filter
		$this->fx_filter->filter ( $filters, $this->manga_model );
		
		// Get Order Params
		$order = $this->input->post ( 'mangaOrder' );
		// Sort
		$this->fx_sort->sort ( $order, $this->manga_model );
		// Get total found records
		$found = $this->manga_model->countTotal ();
		
		// Paginate
		$this->load->library ( "tree/paginator/engine/st_paginator_collection" );
		$this->load->library ( "tree/paginator/st_paginator" );
		$this->st_paginator_collection->setModel ( $this->manga_model );
		$this->st_paginator->setConfig ( array ('engine' => $this->st_paginator_collection, 'pageRange' => $this->module ['grid'] ['page_range'], 'itemCountPerPage' => $recordPerPage, 'currentPageNumber' => $page ) );
		
		// Paginate Collection
		$collection = $this->st_paginator->getItemsByPage ( $page );
		if (empty ( $collection )) {
			$collection = array ();
		}
		$data = array ();
		$data ['found'] = $found;
		$data ['sorter'] = $this->fx_sort;
		$data ['filters'] = $this->fx_filter;
		$data ['paginator'] = $this->st_paginator;
		$data ['manga_ids'] = $this->input->post ( 'manga_ids' );
		$data ['manga_ids_array'] = explode ( ',', $this->input->post ( 'manga_ids' ) );
		$data ['collection'] = $collection;
		return $data;
		
	}
	/*
	public function delete() {
		try {
			if ($this->manga_model->isLoaded ()) {
				$name = $this->manga_model->getName ();
				$this->manga_model->delete ();
				$this->st_registry->register ( 'admin/catalog/manga/message', "Delete manga [$name] successfully" );
				redirect ( site_url ( "admin/catalog/manga/index", true ) );
			} else {
				$this->st_registry->register ( 'admin/catalog/manga/message', "Cannot delete manga" );
				redirect ( site_url ( "admin/catalog/manga/index", true ) );
			}
		} catch ( Exception $ex ) {
			$this->st_registry->register ( 'admin/catalog/manga/message', "Error when deleting manga" );
			redirect ( site_url ( "admin/catalog/manga/index", true ) );
		}
	}
	
	public function massActive() {
		try {
			// Load Manga Model
			$ids = $this->input->getMassValues ( 'manga_ids' );
			if ($ids) {
				$this->manga_model->query ( "update catalog_manga set is_active = 1 where id in ($ids)" );
			}
			$countItem = count ( explode ( ',', $ids ) );
			$this->st_registry->register ( 'admin/catalog/manga/message', "Active [$countItem] items successfully" );
			redirect ( site_url ( "admin/catalog/manga/index", true ) );
		} catch ( Exception $ex ) {
			$this->st_registry->register ( 'admin/catalog/manga/message', $e->getMessage () );
			redirect ( site_url ( "admin/catalog/manga/index", true ) );
		}
	}
	
	public function massDeactive() {
		try {
			// Load Manga Model
			$ids = $this->input->getMassValues ( 'manga_ids' );
			if ($ids) {
				$this->manga_model->query ( "update catalog_manga set is_active = 0 where id in ($ids)" );
			}
			$countItem = count ( explode ( ',', $ids ) );
			$this->st_registry->register ( 'admin/catalog/manga/message', "DeActive [$countItem] items successfully" );
			redirect ( site_url ( "admin/catalog/manga/index", true ) );
		} catch ( Exception $ex ) {
			$this->st_registry->register ( 'admin/catalog/manga/message', $e->getMessage () );
			redirect ( site_url ( "admin/catalog/manga/index", true ) );
		}
	}
	
	public function massPublish() {
		try {
			// Load Manga Model
			$ids = $this->input->getMassValues ( 'manga_ids' );
			$publishDay = $this->input->post ( 'publishDay' );
			if ($ids) {
				$this->manga_model->query ( "update catalog_manga set is_publish = 1, publish_day = \"$publishDay\" where id in ($ids)" );
			}
			$countItem = count ( explode ( ',', $ids ) );
			$this->st_registry->register ( 'admin/catalog/manga/message', "Publish [$countItem] items successfully" );
			redirect ( site_url ( "admin/catalog/manga/index", true ) );
		} catch ( Exception $ex ) {
			$this->st_registry->register ( 'admin/catalog/manga/message', $e->getMessage () );
			redirect ( site_url ( "admin/catalog/manga/index", true ) );
		}
	}
	
	public function massUnpublish() {
		try {
			// Load Manga Model
			$ids = $this->input->getMassValues ( 'manga_ids' );
			if ($ids) {
				$this->manga_model->query ( "update catalog_manga set is_publish = 0 where id in ($ids)" );
			}
			$countItem = count ( explode ( ',', $ids ) );
			$this->st_registry->register ( 'admin/catalog/manga/message', "Unpublish [$countItem] items successfully" );
			redirect ( site_url ( "admin/catalog/manga/index", true ) );
		} catch ( Exception $ex ) {
			$this->st_registry->register ( 'admin/catalog/manga/message', $e->getMessage () );
			redirect ( site_url ( "admin/catalog/manga/index", true ) );
		}
	}
	
	public function massSetCategory() {
		// Step by step to binding categories and manga
		try {
			// Load Manga Model
			$ids = $this->input->getMassValues ( 'manga_ids' );
			$categoryIds = $this->input->post ( 'categorySetterId' );
			if ($ids && $categoryIds) {
				$ids = explode ( ',', $ids );
				$categoryIds = explode ( ',', $categoryIds );
				foreach ( $ids as $mangaId ) {
					$query = 'select manga_id from catalog_category_manga where manga_id = ' . $mangaId;
					foreach ( $categoryIds as $categoryId ) {
						$query .= ' and category_id = ' . $categoryId;
						$res = $this->manga_model->query ( $query );
						if ($res->num_rows () == 0) {
							$insertQuery = "insert into catalog_category_manga values (NULL, $categoryId, $mangaId, 1)";
							$this->manga_model->query ( $insertQuery );
						}
					}
				}
				$countItem = count ( $ids );
				$this->st_registry->register ( 'admin/catalog/manga/message', "Set category for [$countItem] manga successfully" );
				redirect ( site_url ( "admin/catalog/manga/index", true ) );
			} else {
				redirect ( site_url ( "admin/catalog/manga/index", true ) );
			}
		} catch ( Exception $ex ) {
			$this->st_registry->register ( 'admin/catalog/manga/message', $e->getMessage () );
			redirect ( site_url ( "admin/catalog/chapter/index", true ) );
		}
	}
	
	public function save() {
		try {
			// Load Manga Model
			$id = $this->input->post ( 'id' );
			if ($id) {
				$this->manga_model->loadByPK ( $id );
			} else {
				$this->manga_model->setCreatedAt ( "NOW()" );
			}
			// Get Entire Data
			$data = $this->input->post ( 'manga' );
			if (is_array ( $data ) && count ( $data ) > 0) {
				// Set Entire Data
				$this->manga_model->setEntireData ( $data );
				
				//Upload Thumb Image
				$config ['upload_path'] = './media/manga/thumbs/';
				$config ['allowed_types'] = 'gif|jpg|png|bmp';
				$config ['max_size'] = '500';
				$this->upload->initialize ( $config );
				$this->upload->do_upload ( 'manga_thumb' );
				$dataUpload = $this->upload->data ();
				$thumbPath = $config ['upload_path'] . $dataUpload ['file_name'];
				if (strlen ( $dataUpload ['file_name'] ) > 0) {
					$this->manga_model->setThumbImage ( $thumbPath );
				}
				
				//Upload Cover Image
				$config ['upload_path'] = './media/manga/covers/';
				$this->upload->initialize ( $config );
				$this->upload->do_upload ( 'manga_cover' );
				$dataUpload = $this->upload->data ();
				$coverPath = $config ['upload_path'] . $dataUpload ['file_name'];
				if (strlen ( $dataUpload ['file_name'] ) > 0) {
					$this->manga_model->setCoverImage ( $coverPath );
				}
				$this->manga_model->setUpdatedAt ( "NOW()" );
				$this->manga_model->save ();
			}
			// Set Category Ids
			$categoryIds = ($this->input->post ( 'categories' ));
			$this->manga_model->query ( 'delete from catalog_category_manga where manga_id = ' . $this->manga_model->getId () );
			if (count ( $categoryIds ) > 0) {
				foreach ( $categoryIds as $catId ) {
					$this->manga_model->query ( "insert into catalog_category_manga(category_id, manga_id) values ($catId, " . $this->manga_model->getId () . ")" );
				}
				$this->manga_model->updateField ( 'category_ids', implode ( ',', $categoryIds ) );
			}
			$this->st_registry->register ( 'admin/catalog/manga/success', 'Save Manga Information Successfull' );
			redirect ( site_url ( "admin/catalog/manga/edit/id/" . $this->manga_model->getId (), true ) );
		} catch ( Exception $ex ) {
			$this->st_registry->register ( 'admin/catalog/manga/error', $ex->getMessage () );
			redirect ( site_url ( "admin/catalog/manga/index", true ) );
		}
	}
	
	public function generalTab() {
		//Get Data
		$data = $this->_loadData ();
		$this->load->view ( 'catalog/manga/edit/form', $data );
	}
	
	public function summaryTab() {
		//Get Data
		$data = $this->_loadData ();
		$this->load->view ( 'catalog/manga/edit/summary', $data );
	}
	
	public function categoriesTab() {
		//Get Data
		$data = $this->_loadData ();
		$this->load->view ( 'catalog/manga/edit/categories', $data );
	}
	
	public function publishTab() {
		//Get Data
		$data = $this->_loadData ();
		$this->load->view ( 'catalog/manga/edit/publish', $data );
	}
	
	public function metaTab() {
		//Get Data
		$data = $this->_loadData ();
		$this->load->view ( 'catalog/manga/edit/meta', $data );
	}
	*/
}